/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.business.param.business;

import com.shop.cereshop.commons.config.RsaProperties;
import com.shop.cereshop.commons.utils.RsaUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.Pattern;

/**
 * 商家登录请求
 */
@Slf4j
@Data
@ApiModel(value = "BusinessLoginParam", description = "商家登录请求")
public class BusinessLoginParam {

  /**
   * 用户名
   */
  @ApiModelProperty(value = "用户名")
  private String username;

  /**
   * 密码
   */
  @ApiModelProperty(value = "密码")
  private String password;

  /**
   * 验证码
   */
  @ApiModelProperty(value = "验证码")
  private String code;

  /**
   * 是否7日内自动登录
   */
  @ApiModelProperty(value = "是否7日内自动登录")
  private Boolean rememberMe;

  /**
   * RSA解密
   *
   * @return 解密对象
   * @throws Exception
   */
  public BusinessLoginParam decrypt() throws Exception {
    BusinessLoginParam res = new BusinessLoginParam();
    try {
      if (this.username != null && !this.username.isEmpty()) {
        String decryptedUsername = RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, this.username);
        if (decryptedUsername == null || decryptedUsername.isEmpty()) {
          throw new Exception("用户名解密结果为空");
        }
        res.setUsername(decryptedUsername);
      }
      if (this.password != null && !this.password.isEmpty()) {
        String decryptedPassword = RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, this.password);
        if (decryptedPassword == null || decryptedPassword.isEmpty()) {
          throw new Exception("密码解密结果为空");
        }
        res.setPassword(decryptedPassword);
      }
      if (this.code != null && !this.code.isEmpty()) {
        res.setCode(this.code);
      }
      if (this.rememberMe != null) {
        res.setRememberMe(this.rememberMe);
      }
    } catch (Exception e) {
      log.error("RSA解密失败: ", e);
      throw e;
    }
    return res;
  }
}
